<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
    <template #value_limit_json="{row}">
      <div >

      </div>
    </template>
  </forms>
</template>

<script>
import s94Mjs from "s94-js";
import {iframe} from "@/js/tool.js";
import {ajax} from "@/js/api_tool.js";
import {_config, decode_config} from "@/js/s94_tool.js";
import {S94Model} from "@/js/S94Model.js";

export default {
  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;
    let model_name_options = decode_config(_config('model_map'), {content_key:'label', value_key:'name'})
    let value_limit_config = {
      status: {name:'status', label:'支付状态', type:'checkbox', config:[]},
      pay_status: {name:'pay_status', label:'支付状态', type:'checkbox', config:[]},
    }
    let forms_config = {
      model_name: {name:'model_name', label:'模型名', type:'select_one', config:model_name_options},
      group: {name:'group', label:'枚举组', type:'text_one'},
      title: {name:'title', label:'标题', type:'text_one'},
      value_limit_json: {name:'value_limit_json', label:'值限制', type:'json_param', config: value_limit_config},
    }
    if (id){
      forms_config.create_time = {label:'创建时间',name:'create_time',tips:''};
    }
    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      id: id,
      btns: btns,
      config: forms_config,
    }
  },
  watch: {
    'config.model_name.value'(v){
      let md = S94Model.find(v);
      let pay_status_options = [];
      let status_options = [];
      if (md){
        pay_status_options = decode_config(md.status_alias_map.pay_status);
        status_options = decode_config(md.status_alias_map.status);
      }
      this.config.value_limit_json.config.status.config = status_options;
      this.config.value_limit_json.config.pay_status.config = pay_status_options;
    }
  },
  methods: {
    onsubmit(formData){
      let url = this.id ? `/admin/enum_title/edit?id=`+this.id : '/admin/enum_title/add';
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('enum_title/index', '$refs.list_url.list');
          iframe.hide('enum_title/edit');
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })
    }
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id) {
      ajax({url:'/admin/enum_title/info?id='+this.id, cache:true}, (res)=>{
        this.$refs['forms'].init(res.data);
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('enum_title/edit');
        }else {
          this.$router.push('index');
        }
      })
    }

    //其他初始化操作
  }
}
</script>
